<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="客户编号" prop="cusId">
        <el-input
          v-model="queryParams.cusId"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="cusName">
        <el-select style="width:100%"  clearable filterable  v-model="queryParams.cusName" placeholder="请选择客户">
          <el-option
            v-for="dict in jcCusInfoList"
            :key="dict.id"
            :label="dict.cusName"
            :value="dict.cusName"
          ></el-option>
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.cusName"-->
<!--          placeholder="请输入客户名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单标题" prop="orderTitle">
        <el-input
          v-model="queryParams.orderTitle"
          placeholder="请输入订单标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最近回款时间" prop="lastTime">
        <el-date-picker clearable
          v-model="queryParams.lastTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最近回款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="flag">
        <el-select v-model="queryParams.flag" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.hk_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:hkmain:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:hkmain:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:hkmain:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:hkmain:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hkmainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="回款编号" align="center" prop="id" />
<!--      <el-table-column label="公司编号" align="center" prop="deptId" />-->
    <!--  <el-table-column label="客户编号" align="center" prop="cusId" />-->
      <el-table-column label="客户名称" align="center" prop="cusName" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="订单标题" align="center" prop="orderTitle" />
      <el-table-column label="回款总金额" align="center" prop="hkTotalMoney" />
      <el-table-column label="最近回款时间" align="center" prop="lastTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="flag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hk_flag" :value="scope.row.flag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作"  width="300px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="toInfo(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="addInfo(scope.row)"
          >新增明细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:hkmain:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:hkmain:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改回款主表对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
<!--        <el-form-item label="公司编号" prop="deptId">-->
<!--          <el-input v-model="form.deptId" placeholder="请输入公司编号" />-->
<!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户编号" prop="cusId">
              <el-input disabled v-model="form.cusId" placeholder="请输入客户编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="cusName">
              <el-select  @change="cusChange1" style="width:100%"  clearable filterable  v-model="form.cusName" placeholder="请选择客户">
                <el-option
                  v-for="dict in jcCusInfoList"
                  :key="dict.id"
                  :label="dict.cusName"
                  :value="{'lable':dict.cusName,'value':dict.id}"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单编号" prop="orderId">
              <el-select  @change="cusChange2" style="width:100%"  clearable filterable  v-model="form.orderId" placeholder="请选择订单">
                <el-option
                  v-for="dict in orderinfoList"
                  :key="dict.id"
                  :label="dict.id"
                  :value="{'lable':dict.cusName,'value':dict.id,'value2':dict.companyName}"
                ></el-option>
              </el-select>
<!--              <el-input v-model="form.orderId" placeholder="请输入订单编号" />-->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单标题" prop="orderTitle">
              <el-input disabled v-model="form.orderTitle" placeholder="请输入订单标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="回款总金额" prop="hkTotalMoney">
              <el-input v-model="form.hkTotalMoney" placeholder="请输入回款总金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最近回款时间" prop="lastTime">
              <el-date-picker clearable
                              v-model="form.lastTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择最近回款时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="flag">
              <el-select v-model="form.flag" placeholder="请选择状态">
                <el-option
                  v-for="dict in dict.type.hk_flag"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!--    汇款单详情-->
    <el-dialog :close-on-click-modal="false" :title="titleHui" :visible.sync="openHui" width="1200px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="xiangAdd"
            v-hasPermi="['business:hkinfo:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single2"
            @click="xiangUpdate"
            v-hasPermi="['business:hkinfo:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple2"
            @click="xiangDelete"
            v-hasPermi="['business:hkinfo:remove']"
          >删除</el-button>
        </el-col>
        <!--        <el-col :span="1.5">-->
        <!--          <el-button-->
        <!--            type="warning"-->
        <!--            plain-->
        <!--            icon="el-icon-download"-->
        <!--            size="mini"-->
        <!--            @click="handleExport"-->
        <!--            v-hasPermi="['base:projectinfo:export']"-->
        <!--          >导出</el-button>-->
        <!--        </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList2"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="hkinfoList" @selection-change="handleSelectionChange2">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="回款明细编号" align="center" prop="id" />
        <el-table-column label="公司编号" align="center" prop="deptId" />
        <el-table-column label="主表编号" align="center" prop="mainId" />
        <el-table-column label="客户编号" align="center" prop="cusId" />
        <el-table-column label="客户名称" align="center" prop="cusName" />
        <el-table-column label="回款金额" align="center" prop="res1" />
        <!--<el-table-column label="回款方式" align="center" prop="hk_type" />-->
        <el-table-column label="回款方式" align="center" prop="hkType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.hk_type" :value="scope.row.hkType"/>
          </template>
        </el-table-column>
        <el-table-column label="回款时间" align="center" prop="hkTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.hkTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="回款人" align="center" prop="hkName" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeD">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改项目详情对话框 -->
    <el-dialog :close-on-click-modal="false" title="添加项目详情" :visible.sync="openXiangInfo" width="500px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules" label-width="80px">
<!--        <el-form-item label="公司编号" prop="deptId">-->
<!--          <el-input v-model="form2.deptId" placeholder="请输入公司编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="主表编号" prop="mainId">-->
<!--          <el-input v-model="form2.mainId" placeholder="请输入主表编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="客户编号" prop="cusId">
          <el-input disabled v-model="form2.cusId" placeholder="请输入客户编号" />
        </el-form-item>
        <el-form-item label="客户名称" prop="cusName">
<!--          <el-input v-model="form2.cusName" placeholder="请输入客户名称" />-->
          <el-select :disabled="disabled"  @change="cusChange" style="width:100%"  clearable filterable  v-model="form2.cusName" placeholder="请选择客户">
            <el-option
              v-for="dict in jcCusInfoList"
              :key="dict.id"
              :label="dict.cusName"
              :value="{'lable':dict.cusName,'value':dict.id}"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回款金额" prop="res1">
          <el-input v-model="form2.res1" placeholder="请输入回款金额" />
        </el-form-item>
        <el-form-item label="回款方式" prop="hkType">
          <el-select v-model="form2.hkType" placeholder="请选择回款方式">
            <el-option
              v-for="dict in dict.type.hk_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回款时间" prop="hkTime">
          <el-date-picker clearable
                          v-model="form2.hkTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择回款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="回款人" prop="hkName">
          <el-input v-model="form2.hkName" placeholder="请输入回款人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form2.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHkmain, getHkmain, delHkmain, addHkmain, updateHkmain } from "@/api/business/hkmain";
import { listHkinfo, getHkinfo, delHkinfo, addHkinfo, updateHkinfo } from "@/api/business/hkinfo";
import { listJcCusInfo } from "@/api/base/jcCusInfo";
import { listOrderinfo } from "@/api/business/orderinfo";

export default {
  name: "Hkmain",
  dicts: ['hk_flag','hk_type'],
  data() {
    var validateRes = (rule, value, callback) => {
      var reg = /^(0|[1-9]\d{0,17})(\.\d{1,2})?$/;
      if(!reg.test(value) && value!=undefined && value!=""){
        return callback(new Error('只能输入数值保留两位小数'));
      } else{
        callback();
      }
    };
    var validateMoney = (rule, value, callback) => {
      var reg = /^(0|[1-9]\d{0,17})(\.\d{1,2})?$/;
      if(!reg.test(value) && value!=undefined && value!=""){
        return callback(new Error('只能输入数值保留两位小数'));
      } else{
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      ids2: [],
      // 非单个禁用
      single: true,
      single2: true,
      // 非多个禁用
      multiple: true,
      multiple2: true,
      // 显示搜索条件
      showSearch: true,
      showSearch2: true,
      // 总条数
      total: 0,
      total2: 0,
      disabled:true,
      //回款单详情开关
      openHui:false,
      openXiangInfo:false,
      // 回款主表表格数据
      hkmainList: [],
      hkinfoList:[],
      jcCusInfoList:[],
      orderinfoList:[],
      // 弹出层标题
      title: "",
      titleHui:'回款单详情',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        cusId: null,
        cusName: null,
        orderId: null,
        orderTitle: null,
        hkTotalMoney: null,
        lastTime: null,
        flag: null,
        res1: null,
        res2: null,
        res3: null
      },
      // 查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        mainId: null,
        cusId: null,
        cusName: null,
        hkType: null,
        hkTime: null,
        hkName: null,
        res1: null,
        res2: null,
        res3: null
      },
      // 查询参数
      queryParams3: {
        pageNum: 1,
        pageSize: 1000000000000000000,
        deptId: null,
        cusName: null,
        companyName: null,
        cusType: null,
        cusAddress: null,
        cusContact: null,
        res1: null,
        res2: null,
        res3: null
      },
      // 查询参数
      queryParams4: {
        pageNum: 1,
        pageSize: 100000000000000000000000000000,
        companyName: null,
        cusId: null,
        cusName: null,
        orderTitle: null,
        kpDate: null,
        address: null,
        pNum: null,
        goodNum: null,
        itemCode: null,
        itemName: null,
        spec: null,
        unit: null,
        price: null,
        money: null,
        res1: null,
        res2: null,
        res3: null
      },
      // 表单参数
      form: {},
      form2: {},
      // 表单校验
      rules: {
        // deptId: [
        //   { required: true, message: "公司编号不能为空", trigger: "blur" }
        // ],
        cusName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        res1: [
          { validator: validateRes,trigger: "change" }
        ],
        hkTotalMoney: [
          { validator: validateMoney,trigger: "change" }
        ],
      },
      rules2: {
        // mainId: [
        //   { required: true, message: "项目编号不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
    listJcCusInfo(this.queryParams3).then(response => {
      this.jcCusInfoList = response.rows;
    });
    listOrderinfo(this.queryParams4).then(response => {
      this.orderinfoList = response.rows;
    });
  },
  methods: {
    cusChange1(value){
      this.form.cusName = value.lable
      this.form.cusId = value.value
    },
    cusChange2(value){
      this.form.res1 = value.lable
      this.form.orderId = value.value
      this.form.orderTitle = value.value2
    },
    cusChange(value){
      this.form2.cusName = value.lable
      this.form2.cusId = value.value
    },
    //新增项目
    xiangAdd(){
      // this.reset2();
      this.disabled = false;
      this.openXiangInfo = true;
      // this.titleX = "添加项目详情";
    },
    // 查看详情
    toInfo(row){
     this.form2.mainId = row.id
      this.queryParams2.mainId=row.id
      listHkinfo(this.queryParams2).then(res=>{
        this.openHui = true
        this.hkinfoList = res.rows
      })
    },
    closeD(){
      this.openHui = false
      this.getList()
    },
    /** 查询回款主表列表 */
    getList() {
      this.loading = true;
      listHkmain(this.queryParams).then(response => {
        this.hkmainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getList2() {
      // this.loading = true;
      listHkinfo(this.queryParams2).then(response => {
        this.hkinfoList = response.rows;
        this.total2 = response.total;
        // this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel2(){
      this.openXiangInfo = false;
      this.reset2();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        cusId: null,
        cusName: null,
        orderId: null,
        orderTitle: null,
        hkTotalMoney: null,
        lastTime: null,
        flag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        res1: null,
        res2: null,
        res3: null
      };
      this.resetForm("form");
    },
    // 表单重置
    reset2() {
      this.form2 = {
        id: null,
        deptId: null,
        mainId: null,
        cusId: null,
        cusName: null,
        hkType: null,
        hkTime: null,
        hkName: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        res1: null,
        res2: null,
        res3: null
      };
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleSelectionChange2(selection) {
      this.ids2 = selection.map(item => item.id)
      this.single2 = selection.length!==1
      this.multiple2 = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加回款主表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHkmain(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改回款主表";
      });
    },
    xiangUpdate(row) {
      this.reset2();
      const id = row.id || this.ids2
      getHkinfo(id).then(response => {
        this.form2 = response.data;
        this.openXiangInfo = true;
        // this.title = "修改项目详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHkmain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHkmain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm2() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          if (this.form2.id != null) {
            updateHkinfo(this.form2).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openXiangInfo = false;
              this.getList2();
            });
          } else {
            addHkinfo(this.form2).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openXiangInfo = false;
              this.getList2();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除回款主表编号为"' + ids + '"的数据项？').then(function() {
        return delHkmain(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    xiangDelete(row) {
      const ids = row.id || this.ids2;
      this.$modal.confirm('是否确认删除项目详情编号为"' + ids + '"的数据项？').then(function() {
        return delHkinfo(ids);
      }).then(() => {
        this.getList2();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/hkmain/export', {
        ...this.queryParams
      }, `hkmain_${new Date().getTime()}.xlsx`)
    },
    addInfo(row){
      this.form2.cusId = row.cusId
      this.form2.cusName = row.cusName
      this.form2.mainId = row.id
      this.disabled = true
      this.openXiangInfo = true
    }
  }
};
</script>
